package gfx;

import java.awt.*;
import java.io.File;

import javax.swing.*;

public class GraphicImage {
	
	/***   ATTRIBUTS ***/
	
	private Image image = null; // image of sprites
	private  int xOffset; // lag's sprite (in x) in the image (in pixels)
	private  int yOffset; // lag's sprite (in y) in the image (in pixels)
	private int nbRows; // number of rows in the sprites's image
	private int nbColumns; //  number of columns in the sprites's image
	
	/*** FIN ATTRIBUTS ***/
	
	
	/*** ACCESSEURS ***/
	
	public int getWidth() {
		return image.getWidth(null);
	}
	
	public int getHeight() {
		return image.getHeight(null);
	}
	
	public Image getImage() {
		return image;
	}
	
	public void setImage(String address_image) {
		if ( address_image == null )
			throw new IllegalArgumentException ("Classe GraphicImage [methode setImage] invalid address (values null)");
		else
		{
			File file = new File(address_image);
			if(file.exists() == false) {
				throw new IllegalArgumentException("File " + address_image + " does not exists");
			}
			this.image = new ImageIcon(address_image).getImage();
			while(getWidth() <= 0) {}
			while(getHeight() <= 0) {}
		}
	}  
	
	public int getXOffset() {
		return xOffset;
	}
	
	public void setXOffset(int x_offset) throws IllegalArgumentException  {
		this.xOffset = x_offset; //aucune condition particulière
	}
	
	public int getYOffset() {
		return yOffset;
	}
	
	public void setYOffset(int y_offset) {
		this.yOffset = y_offset; //aucune condition particulière
	}
	
	public int getNbRows() {
		return nbRows;
	}
	
	public void setNbRows(int nb_rows) throws IllegalArgumentException{
		if (nb_rows > 0)	
			this.nbRows = nb_rows;
		else
			throw new IllegalArgumentException ("Class GraphicImage [method setNB_rows] nb_rows is invalid (value < 0)");
	}
	
	public int getNbColumns() {
		return nbColumns;
	}
	
	public void setNbColumns(int nb_columns) {
		if (nb_columns > 0)	
			this. nbColumns =  nb_columns;
		else
			throw new IllegalArgumentException ("Class GraphicImage [method setNB_columns]  nb_columns is invalid (value < 0)");
	} 
	
	/*** FIN ACCESSEURS ***/
	
	
	/*** CONSTRUCTEUR ***/
	
	public GraphicImage ( String address_image, int x_offset, int y_offset, int nb_rows, int nb_columns ){
		//JFrame frame = new JFrame ("blabla");
		
		this.setImage(address_image);
		this.setXOffset(x_offset);
		this.setYOffset(y_offset);
		this.setNbRows(nb_rows);
		this.setNbColumns(nb_columns);
	}

	public GraphicImage(String string) {
		this(string, 0, 0, 1, 1);
	}
	
	/*** FIN CONSTRUCTEUR 
	 * @throws IOException ***/
	
/*	public static void main (String [] args) throws IOException{
		// on initialise un graph_image
		GraphicImage graph_image = new GraphicImage ("gui_font_9x16.bmp",0,0, 32,8);
		
		//on ouvre un jframe
		JFrame frame = new JFrame ("test");
		frame.setLayout(null);
		frame.setBounds(500,100,700,700);
		
		//on créé un conteneur
		Container cont = frame.getContentPane();
		
		//on créé un bouton (pour tester l'ajout)
		JButton button = new JButton ("test");
		button.setBounds(0,0,100,100);
		cont.add(button);
		
		//On crée un JLabel pour test l'image du GraphicImage
		JLabel label = new JLabel ( new ImageIcon (graph_image.getImage()));
		
		if (label == null)
			JOptionPane.showMessageDialog(null, "Errer JLabel","Erreur",JOptionPane.ERROR_MESSAGE);
		else{
			label.setBounds(0,150,500,500);
			cont.add(label);
		}
		
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}*/
}